const app = getApp()
Component({
    data: {
        userInfo: {},
        formData: {
            isResident: '1'
        },
        error: '',
        isResidentItems: [{
                name: '是',
                value: '1',
                checked: true
            },
            {
                name: '否',
                value: '0'
            }
        ],
        rules: [{
            name: 'name',
            rules: {
                required: true,
                message: '姓名必填'
            },
        }, {
            name: 'userId',
            rules: [{
                required: true,
                message: '身份证必填'
            },{
                minlength: 18,
                message: '身份证为18位'
            },{
                maxlength: 18,
                message: '身份证应为18位'
            }],
        }, {
            name: 'phone',
            rules: [{
                required: true,
                message: '电话必填'
            },{
                mobile: true,
                message: '请仔细检查手机号'
            }],
        }, {
            name: 'address',
            rules: {
                required: true,
                message: '地址必填'
            },
        }]
    },
    
    methods: {
        onLoad() {
            this.setData({
                userInfo: wx.getStorageSync('userInfo'),
                [`formData.openid`]: wx.getStorageSync('userInfo').openid
            })
        },
        isResidentChange: function (e) {
            var isResidentItems = this.data.isResidentItems;
            for (var i = 0, len = isResidentItems.length; i < len; ++i) {
                isResidentItems[i].checked = isResidentItems[i].value == e.detail.value;
            }
            this.setData({
                isResidentItems: isResidentItems,
                [`formData.isResident`]: e.detail.value
            });
        },
        formInputChange(e) {
            const { field } = e.currentTarget.dataset
            this.setData({
                [`formData.${field}`]: e.detail.value
            })
        },
        submitForm() {
            this.selectComponent('#form').validate((valid, errors) => {
                //是否有错误
                if (!valid) {
                    const firstError = Object.keys(errors)
                    if (firstError.length) {
                        this.setData({
                            error: errors[firstError[0]].message,
                        })
                    }else{
                    }
                } else {
                    const that = this
                    wx.request({
                        url: app.globalData.serverUrl + '/user/register',
                        method: 'put',
                        header: {
                            'content-type': 'application/x-www-form-urlencoded'
                        },
                        data: {
                            openid: that.data.formData.openid,
                            name: that.data.formData.name,
                            phone: that.data.formData.phone,
                            address: that.data.formData.address,
                            userId: that.data.formData.userId,
                            isResident: that.data.formData.isResident
                        },
                        success: function (res) {
                            if (res.data.status == 200) {
                                wx.showToast({
                                    title: '注册成功',
                                    duration: 1500
                                })
                                that.data.userInfo.name = that.data.formData.name
                                that.data.userInfo.phone = that.data.formData.phone
                                that.data.userInfo.address = that.data.formData.address
                                that.data.userInfo.userId = that.data.formData.userId
                                that.data.userInfo.isResident = that.data.formData.isResident
                                wx.setStorageSync('userInfo', that.data.userInfo)
                                wx.redirectTo({
                                    url: '/pages/wait/wait',
                                })
                                // if(!that.data.userInfo.role){
                                //     wx.redirectTo({
                                //         url: '/pages/home-client/home-client',
                                //     })
                                // }else{
                                //     wx.redirectTo({
                                //         url: '/pages/home-checker/home-checker',
                                //     })
                                // }
                            } else {
                                wx.showToast({
                                    title: '注册失败,请确保填写数据正确',
                                    icon: 'none',
                                    duration: 2000
                                })
                            }
                        },
                        fail: function (error) {
                            //调用服务端登录接口失败
                            wx.showToast({
                                title: '请确保网络通畅',
                                icon: 'none',
                                duration: 2000
                            })
                        }
                    })

                }
            })
            
        }

    }
});